<?php

class bpArticles {
    
    function validateCategoryId($id) {
        if (!empty($id) && is_numeric($id)) {
            return true;
        } else {
            return false;
        }
    }
    
    function validateCategoryAlias($alias, $isSet = false) {
        if (preg_match("/[a-zA-Z0-9]{1,50}/", $alias)) {
            if ($isSet) {
                $result = mysql_query(sprintf("SELECT id FROM article_categories WHERE alias='%s'", mysql_real_escape_string($alias)));
                if (mysql_num_rows($result)) {
                    return mysql_result($result, 0, "id");
                } else {
                    return false;
                }
            } else {
             return true;
            }
        } else {
            return false;
        }
    }
    
    function getCategories($id = 0, $byAlias = false) {
        $where = array();
        if (!empty($id)) {
            if ($byAlias) {
                if (bpArticles::validateCategoryAlias($id)) {
                    $where[] = "alias='$id'";
                } else {
                    return false;
                }
            } else {
                if (bpArticles::validateCategoryId($id)) {
                    $where[] = "id='$id'";
                } else {
                    return false;
                }
            }
        }
        $result = mysql_query("SELECT * FROM article_categories ".($where?"WHERE ".implode(" AND ", $where):""));
        if ($id) {
            $output = mysql_fetch_assoc($result);
        } else {
        $output = array();
            while ($row = mysql_fetch_assoc($result)) {
                $output[] = $row;
            }
        }
        return $output;
    }
    
    function getArticlesByCategory($id) {
        if (bpArticles::validateCategoryId($id)) {
            $output = array();
            $result = mysql_query("SELECT * FROM articles WHERE category_id=".(int)$id);
            while ($row = mysql_fetch_assoc($result)) {
                $row["name"] = htmlspecialchars($row["name"]);
                $output[] = $row;
            }
            return $output;
        } else {
            return false;
        }
    }
    
    function getArticleByAlias($alias) {
        if (bpArticles::validateCategoryAlias($alias)) {
            $output = array();
            $result = mysql_query("SELECT * FROM articles WHERE alias='$alias'");
            $output = mysql_fetch_assoc($result);
            $output["name"] = htmlspecialchars($output["name"]);
            return $output;
        } else {
            return false;
        }
    }
    
    function addCategory($admin_id, $name, $alias, $descr, $visible) {
    	$sql = "INSERT INTO article_categories(admin_id, created, modified, name, alias, descr, visible)
    			VALUES(%d, NOW(), NOW(), '%s', '%s', '%s', %d)";
    	$result = mysql_query(sprintf($sql, $admin_id, $name, $alias, $descr, $visible));
    	
    	return $result;
    }
    
    function editCategory($id, $name, $alias, $descr, $visible) {
    	$sql = "UPDATE article_categories 
    			SET modified = NOW(), name = '%s', alias = '%s', descr = '%s', visible = %d 
    			WHERE id = %d";
    	$result = mysql_query(sprintf($sql, $name, $alias, $descr, $visible, $id));
    	
    	return $result;
    }
    
    function deleteCategory($id) {
    	$sql = "DELETE FROM article_categories WHERE id = %d";
    	$result = mysql_query(sprintf($sql, $id));
    	
    	return $result;
    }
    
    function deleteCategories($ids) {
    	$sql = "DELETE FROM article_categories WHERE id IN('%s')";
    	$result = mysql_query(sprintf($sql, implode(',', $ids)));
    	
    	return $result;
    }
    
}
?>